package com.pansky.crm.util;

import com.alibaba.fastjson.JSONObject;
import com.pansky.crm.constants.CommonConstant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;


/**
 * 延安公共服务接口调用工具类
 */
@Component
public class PublicServicePortUtil {
    @Value("${pansky.upms.gatway.port.url}")
    private String url1;
    private static String url;
    @Value("${pansky.upms.gatway.port.appId}")
    private String appId1;
    private static String appId;
    @Value("${pansky.upms.gatway.port.password}")
    private String password1;
    private static String password;
    //利用@PostConstruct将yml中配置的值赋给本地的变量
    @PostConstruct
    public void getEnvironment(){
        url = this.url1;
        appId = this.appId1;
        password = this.password1;
    }
    public static JSONObject getResult(String differentiationPath, String interfaceName, JSONObject json) throws Exception{
        System.out.println(url +appId+password);
        JSONObject params = new JSONObject();
        params.put("appId", appId);
        params.put("interfaceName", interfaceName);
        String sig = AESUtil.encrypt(json.toString(), password);
        params.put("sig", sig);
        System.out.println(url+differentiationPath+"/"+interfaceName+"?"+params);
        String result = new String(HttpUtil.post(url+differentiationPath, "params="+params.toString()).getBytes(),"UTF-8");
        System.out.println(result);
        //解密
        JSONObject results = JSONObject.parseObject(result);
        System.out.println(AESUtil.decrypt(results.getString("msg"), password, "UTF-8"));
        return  AESUtil.decrypt(results.getString("msg"), password, "UTF-8");
    }

    /**
     * 短信发送
     * @return
     * @throws Exception
     */
    public static JSONObject sendMsg() throws Exception {
        JSONObject json = new JSONObject();
        json.put("content", "验证码：123456，请您在5分钟内填写。如非本人操作，请忽略本短信。");//短信内容
        json.put("mobile", "18602900515");//接受短信的手机号
        JSONObject jsonstr =getResult(CommonConstant.DUANXIN_HYDX,"sendMsg_HY", json);
        System.out.println(jsonstr);
        return jsonstr;
    }


}
